package com.pangyangyang.elasticsearch.plugin.jdbc.pool;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.sql.Connection;

/**
 * Created by yangwang on 2015/1/27.
 */
public class JDBCConnectionProxy implements InvocationHandler {

    private Connection connection;
    private JDBCConnectionPool pool;

    public JDBCConnectionProxy(Connection connection, JDBCConnectionPool pool) {
        this.connection = connection;
        this.pool = pool;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String methodName = method.getName();
        if(methodName.equals("close")){
            pool.releaseConnection((Connection)proxy);
            return null;
        }
        return method.invoke(connection, args);
    }

    public Connection getConnection() {
        return connection;
    }
}
